﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityModel;
using DataAccess;
using Common;
using System.Data.Objects;

namespace Business
{
    public class TicketBC : BusinessBase<Ticket, UserModel>
    {
        public Dictionary<int, List<ChannelOpen>> LookupTickets(string ticketNumber, int areaID, int channelID, DateTime? openDate)
        {
            Func<ChannelOpen, bool> whereExpression = null;

            whereExpression = t => t.Locked == false && (channelID == 0 || t.ChannelID == channelID)
                && (areaID == 0 || t.Channel.AreaChannelRefs.Any(u => u.AreaID == areaID))
                && (openDate==null || EntityFunctions.TruncateTime(t.OpenDate) == openDate);

            if (areaID > 0 && channelID > 0)
                whereExpression = t => t.Locked == false && t.ChannelID == channelID
                                    && t.Channel.AreaChannelRefs.Any(u => u.AreaID == areaID);
            else if (channelID > 0)
                whereExpression = t => t.Locked == false && t.ChannelID == channelID;
            else if (areaID > 0)
                whereExpression = t => t.Locked == false && t.Channel.AreaChannelRefs.Any(u => u.AreaID == areaID);
            else
                whereExpression = t => t.Locked == false;

            return LookupTickets(ticketNumber, whereExpression);
        }

        private Dictionary<int, List<ChannelOpen>> LookupTickets(string ticketNumber, Func<ChannelOpen, bool> whereExpression)
        {
            var searchResult = new Dictionary<int, List<ChannelOpen>>();
            
            ticketNumber = ticketNumber.Trim();
            if (ticketNumber.Length < 2) return searchResult;

            var repChannelOpen = this.retreiveHelper.GetRepositoryWithoutTracking<ChannelOpen>();
            if (whereExpression != null) repChannelOpen = repChannelOpen.Where(whereExpression).AsQueryable();

            var searchValue = ticketNumber.Substring(ticketNumber.Length - 2, 2);
            var matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelEighth == searchValue)).ToList();
            searchResult.Add((int)PRIZE.EIGHTH, matchPrize);

            if (ticketNumber.Length >= 3)
            {
                searchValue = ticketNumber.Substring(ticketNumber.Length - 3, 3);
                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelSeventh == searchValue)).ToList();
                searchResult.Add((int)PRIZE.SEVENTH, matchPrize);
            }

            if (ticketNumber.Length >= 4)
            {
                searchValue = ticketNumber.Substring(ticketNumber.Length - 4, 4);
                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelSixth == searchValue)).ToList();
                searchResult.Add((int)PRIZE.SIXTH, matchPrize);

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelFifth == searchValue)).ToList();
                searchResult.Add((int)PRIZE.FIFTH, matchPrize);
            }

            if (ticketNumber.Length >= 5)
            {
                searchValue = ticketNumber.Substring(ticketNumber.Length - 5, 5);
                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelFourth == searchValue)).ToList();
                searchResult.Add((int)PRIZE.FOURTH, matchPrize);

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelThird == searchValue)).ToList();
                searchResult.Add((int)PRIZE.THIRD, matchPrize);

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelSecond == searchValue)).ToList();
                searchResult.Add((int)PRIZE.SECOND, matchPrize);

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelFirst == searchValue)).ToList();
                searchResult.Add((int)PRIZE.FIRST, matchPrize);

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelSpecial.EndsWith(searchValue))).ToList();
                searchResult.Add((int)PRIZE.LUCKY, matchPrize);
            }

            if (ticketNumber.Length == 6)
            {
                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => u.LevelSpecial == ticketNumber)).ToList();
                searchResult.Add((int)PRIZE.SPECIAL, matchPrize);

                var encourageList = new List<string>();
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}", i, ticketNumber.Substring(1, 5)));
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}{2}", ticketNumber.Substring(0, 1), i, ticketNumber.Substring(2, 4)));
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}{2}", ticketNumber.Substring(0, 2), i, ticketNumber.Substring(3, 3)));
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}{2}", ticketNumber.Substring(0, 3), i, ticketNumber.Substring(4, 2)));
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}{2}", ticketNumber.Substring(0, 4), i, ticketNumber.Substring(5, 1)));
                for (int i = 0; i < 10; i++)
                    encourageList.Add(string.Format("{0}{1}", ticketNumber.Substring(0, 5), i));

                matchPrize = repChannelOpen.Where(t => t.OpenDetails.Any(u => encourageList.Contains(u.LevelSpecial))).ToList();
                searchResult.Add((int)PRIZE.ENCOURAGE, matchPrize);
            }

            return searchResult;
        }

        public Result<Ticket> GetAllByAgency(Int32 agentID, string sortCol, SORT_ORDER sortOrder, Int16 currentPage, Int16 pageNumber)
        {
            Func<Ticket, bool> whereExpression = t => (t.AgentID == agentID && t.Locked == false);
            return this.retreiveHelper.GetAllPaging(whereExpression, sortCol, sortOrder, currentPage, pageNumber);
        }        
    }
}
